/**
	BeanSoft Common JavaScript lib v1.0
	(c) 2005 - 2006 Jacky Liu
	@author Jacky Liu
	Created by Aptana Web Development Tools
	Visit http://www.aptana.com/
*/

/**
 * @constructor
 * This is a ajax form helper class.
 *
 * @param form - the document form
 * @param resultDivId - the result div id
 * @author beansoft
 * @version 2006-05-01
 */
function AjaxFormer (form, resultDivId) {
	/** form instance */
	this.form = form;
	/** div to display result in */
	this.resultDivId = resultDivId;

	/**
	 * This will call the ajax submit method to do the form submit.
	 * @author beansoft
	 * @version 1.0 2006-05-04
	 */
	this.ajaxSubmitForm = function() {
	  if(!ajaxValidate()){
			return;
		}
	  var elements = form.elements;// Enumeration the form elements
	  var element;
	  var i;

	  var postContent = "";// Form contents need to submit

	  for(i=0;i<elements.length;++i) {
	  	var element=elements[i];

	    if(element.type=="text" || element.type=="textarea" || element.type=="hidden") {
	      postContent += encodeURIComponent(element.name) + "=" + encodeURIComponent(element.value) + "&";
	    }
	    else if(element.type=="select-one"||element.type=="select-multiple") {
	      var options=element.options,j,item;
	      for(j=0;j<options.length;++j){
	        item=options[j];
	        if(item.selected) {
	        	postContent += encodeURIComponent(element.name) + "=" + encodeURIComponent(item.value) + "&";
			}
	      }
	    } else if(element.type=="checkbox"||element.type=="radio") {
	      if(element.checked) {
	        	postContent += encodeURIComponent(element.name) + "=" + encodeURIComponent(element.value) + "&";
			}
		} else if(element.type=="file") {
			if(element.value != "") {
				postContent += encodeURIComponent(element.name) + "=" + encodeURIComponent(element.value) + "&";
			}
		} else {
				postContent += encodeURIComponent(element.name) + "=" + encodeURIComponent(element.value) + "&";
		}
	  }

//	  var contentDiv = document.createElement("DIV");
//	  contentDiv.style.border="1px solid #008800";
//	  contentDiv.innerHTML = "Will post:<br/>" + postContent;
//	  document.body.appendChild(contentDiv);

	  this.ajaxSubmit(form.action, form.method, postContent);
	  ajaxSucess();
	}

	/**
	 * Submit the content of a form to a url.
	 * @param url - the url to do submit
	 * @param method - "get" or "post"
	 * @param postContent - the string with values to be submited
	 * @param resultDivId - the division of which to display result text in, in null, then
	 * create an element and add it to the end of the body
	 */
	this.ajaxSubmit = function(url, method, postContent, resultDivId)
	{
		// code for Mozilla, etc.
		if (window.XMLHttpRequest)
		{
			xmlhttp=new XMLHttpRequest();
		}
		// code for IE
		else if (window.ActiveXObject)
		{
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}

		if(xmlhttp) {
			xmlhttp.onreadystatechange = function() {
				// debugger; // Moz debug keyword
				// if xmlhttp shows "loaded"
				if (xmlhttp.readyState==4)
				{
					if(resultDivId) {
						document.getElementByID(resultDivId).innerHTML = xmlhttp.responseText;
					} 
					//else {
	                 //   var result = document.createElement("DIV");
	                 //   result.style.border="1px solid #363636";
	                 //   result.innerHTML = xmlhttp.responseText;
	                 //   document.body.appendChild(result);
					//}

				  	//loadingDiv.innerHTML =
					//  "<img src='./images/good.gif'/>Submit finnished!";
				}

			};

			if(method.toLowerCase() == "get") {
				xmlhttp.open("GET", url + "?" + postContent, true);
				xmlhttp.send(null);
			} else if(method.toLowerCase() == "post") {
				xmlhttp.open("POST", url, true);
				xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
				xmlhttp.send(postContent);
			}
		} else {
	        loadingDiv.innerHTML =
			  "Can't create XMLHttpRequest object, please check your web browser.";
		}

	}
}

// }}} end of common utils methods